package com.uzhie.jt808.jt1078.entity;

import com.uzhie.jt808.JT808MsgBody;
import com.uzhie.jt808.msg.entity.BitConverter;
import com.uzhie.jt808.msg.entity.MyBuffer;
import io.netty.buffer.ByteBufUtil;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/**
 * ftp远程文件上传指令
 * Created by lilongfei on 2018/7/30.
 */
public class JT_9206 implements JT808MsgBody {

    //服务器ip长度
    byte serverIpLength;
    //服务器ip
    String serverIp;
    //服务器端口号
    short port;
    //登录用户名长度
    byte usernameLength;
    //登录用户名
    String username;
    //登录密码长度
    byte pwdLength;
    //登录密码
    String pwd;
    //文件存储地址长度
    byte filePathLength;
    //文件存储地址
    String filePath;
    //逻辑通道号
    byte channel;
    //起始时间
    String startTime;
    //终止时间
    String endTime;
    //报警标识
    byte[] warn;
    //文件类型 0音视频 1音频 2视频 3视频或音频
    byte type;
    //码流类型 0主码流或子码流  1主码流 2子码流
    byte stream;
    //存储位置  0全部 1主存储 2灾备
    byte local;
    //上传条件 1 wifi上传 2LAN上传 4 3G/4G上传
    byte condition;

    public JT_9206() {
    }

    public JT_9206(String ip, short port, String username, String pwd, String filePath, int channel, String startTime, String endTime, int warns, int type, int stream, int local, int condition) {
        setIpLength(ip.length());
        setServerIp(ip);
        setPort(port);
        setUsernameLength(username.length());
        setUsername(username);
        setPwdLength(pwd.length());
        setPwd(pwd);
        setFilePathLength(filePath.length());
        setFilePath(filePath);
        setChannel(channel);
        setStartTime(startTime);
        setEndTime(endTime);
        setWarns(warns);
        setType(type);
        setStream(stream);
        setLocal(local);
        setCondition(condition);
    }

    /**
     * 转换字段ip长度为byte类型,保存在消息体内
     *
     * @param length
     */
    public void setIpLength(int length) {
        byte[] b_Length = BitConverter.GetBytes(length);
        //此处取低8位数据进行传输
        setServerIpLength(b_Length[3]);
    }


    public void setUsernameLength(int length) {
        byte[] b_Length = BitConverter.GetBytes(length);
        //此处取低8位数据进行传输
        setUsernameLength(b_Length[3]);
    }

    public void setPwdLength(int length) {
        byte[] b_Length = BitConverter.GetBytes(length);
        //此处取低8位数据进行传输
        setPwdLength(b_Length[3]);
    }

    public void setFilePathLength(int length) {
        byte[] b_Length = BitConverter.GetBytes(length);
        //此处取低8位数据进行传输
        setFilePathLength(b_Length[3]);
    }

    public void setLocal(int length) {
        byte[] b_Length = BitConverter.GetBytes(length);
        //此处取低8位数据进行传输
        setLocal(b_Length[3]);
    }

    public void setCondition(int length) {
        byte[] b_Length = BitConverter.GetBytes(length);
        //此处取低8位数据进行传输
        setCondition(b_Length[3]);
    }

    public void setWarns(int warn) {
        byte[] warns = BitConverter.GetBytes(warn);
        setWarn(warns);
    }

    /**
     * 转换数字型通道号为byte对象
     *
     * @param channel
     */
    public void setChannel(int channel) {
        byte[] b_Length = BitConverter.GetBytes(channel);
        //此处取低8位数据进行传输
        setChannel(b_Length[3]);
    }

    /**
     * 转换数字型类型为byte对象
     *
     * @param type
     */
    public void setType(int type) {
        byte[] b_Length = BitConverter.GetBytes(type);
        //此处取低8位数据进行传输
        setType(b_Length[3]);
    }

    /**
     * 转换数字型码流为byte对象
     *
     * @param stream
     */
    public void setStream(int stream) {
        byte[] b_Length = BitConverter.GetBytes(stream);
        //此处取低8位数据进行传输
        setStream(b_Length[3]);
    }


    public byte getServerIpLength() {
        return serverIpLength;
    }

    public void setServerIpLength(byte serverIpLength) {
        this.serverIpLength = serverIpLength;
    }

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public short getPort() {
        return port;
    }

    public void setPort(short port) {
        this.port = port;
    }

    public byte getUsernameLength() {
        return usernameLength;
    }

    public void setUsernameLength(byte usernameLength) {
        this.usernameLength = usernameLength;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public byte getPwdLength() {
        return pwdLength;
    }

    public void setPwdLength(byte pwdLength) {
        this.pwdLength = pwdLength;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public byte getFilePathLength() {
        return filePathLength;
    }

    public void setFilePathLength(byte filePathLength) {
        this.filePathLength = filePathLength;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public byte getChannel() {
        return channel;
    }

    public void setChannel(byte channel) {
        this.channel = channel;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public byte[] getWarn() {
        return warn;
    }

    public void setWarn(byte[] warn) {
        this.warn = warn;
    }

    public byte getType() {
        return type;
    }

    public void setType(byte type) {
        this.type = type;
    }

    public byte getStream() {
        return stream;
    }

    public void setStream(byte stream) {
        this.stream = stream;
    }

    public byte getLocal() {
        return local;
    }

    public void setLocal(byte local) {
        this.local = local;
    }

    public byte getCondition() {
        return condition;
    }

    public void setCondition(byte condition) {
        this.condition = condition;
    }

    @Override
    public byte[] WriteToBytes() {
        MyBuffer buffer = new MyBuffer();

        buffer.put(getServerIpLength());
        buffer.put(getServerIp());
        buffer.put(getPort());
        buffer.put(getUsernameLength());
        buffer.put(getUsername());
        buffer.put(getPwdLength());
        buffer.put(getPwd());
        buffer.put(getFilePathLength());
        buffer.put(getFilePath());
        buffer.put(getChannel());
        //设置起始时间
        buffer.put(Byte.parseByte(getStartTime().substring(2, 4), 16));
        buffer.put(Byte.parseByte(getStartTime().substring(5, 7), 16));
        buffer.put(Byte.parseByte(getStartTime().substring(8, 10), 16));
        buffer.put(Byte.parseByte(getStartTime().substring(11, 13), 16));
        buffer.put(Byte.parseByte(getStartTime().substring(14, 16), 16));
        buffer.put(Byte.parseByte(getStartTime().substring(17, 19), 16));
        //设置终止时间
        buffer.put(Byte.parseByte(getEndTime().substring(2, 4), 16));
        buffer.put(Byte.parseByte(getEndTime().substring(5, 7), 16));
        buffer.put(Byte.parseByte(getEndTime().substring(8, 10), 16));
        buffer.put(Byte.parseByte(getEndTime().substring(11, 13), 16));
        buffer.put(Byte.parseByte(getEndTime().substring(14, 16), 16));
        buffer.put(Byte.parseByte(getEndTime().substring(17, 19), 16));

        buffer.put(getWarn());
        buffer.put(getType());
        buffer.put(getStream());
        buffer.put(getLocal());
        buffer.put(getCondition());
        return buffer.array();
    }

    @Override
    public void ReadFromBytes(byte[] messageBodyBytes) {
        Charset charset = Charset.forName("gbk");
        //TODO JT_9206没有实现read方法
        ByteBuffer buffer = ByteBuffer.wrap(messageBodyBytes);
        serverIpLength = buffer.get();
        byte[] serverIpByte = new byte[serverIpLength & 0XFF];
        buffer.get(serverIpByte, 0, serverIpByte.length);
        serverIp = new String(serverIpByte, charset);
        port = buffer.getShort();
        usernameLength = buffer.get();
        byte[] usernameByte = new byte[usernameLength & 0XFF];
        buffer.get(usernameByte, 0, usernameByte.length);
        username = new String(usernameByte, charset);
        pwdLength = buffer.get();
        byte[] pwdByte = new byte[pwdLength & 0XFF];
        buffer.get(pwdByte, 0, pwdByte.length);
        pwd = new String(pwdByte, charset);
        filePathLength = buffer.get();
        byte[] filePathByte = new byte[filePathLength & 0XFF];
        buffer.get(filePathByte, 0, filePathByte.length);
        filePath = new String(filePathByte, charset);
        channel = buffer.get();
        byte[] startTimeByte = new byte[6];
        buffer.get(startTimeByte, 0, 6);
        startTime = ByteBufUtil.hexDump(startTimeByte);
        byte[] endTimeByte = new byte[6];
        buffer.get(endTimeByte, 0, 6);
        endTime = ByteBufUtil.hexDump(endTimeByte);
        warn = new byte[8];
        buffer.get(warn, 0, 8);
        type = buffer.get();
        stream = buffer.get();
        local = buffer.get();
        condition = buffer.get();
    }
}
